package com.ztesoft.ossup.report.dao;

import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.util.StringUtils;

import com.ztesoft.ossup.report.domain.ReportPieChartData;
import com.ztesoft.ossup.report.domain.ReportPieChartData_;

public interface PieChartRepo extends JpaRepository<ReportPieChartData, Long>, JpaSpecificationExecutor<ReportPieChartData> {
	class ReportSpecifications {

		public static Specification<ReportPieChartData> likeReportName(final String name) {
			return new Specification<ReportPieChartData>() {
				@Override
				public Predicate toPredicate(Root<ReportPieChartData> root, CriteriaQuery<?> criteriaQuery,
						CriteriaBuilder criteriaBuilder) {
							if (!StringUtils.isEmpty(name))
								return criteriaBuilder.like(root.get(ReportPieChartData_.label), "%" + name + "%");
							return null;
						}
			};
		}
	}

	List<ReportPieChartData> findAllByChartId(Long chartId);
	
	@Query(value="delete from REPORT_PIE_CHART_DATA T WHERE T.CHART_ID in (select id from report_chart where report_id = ?1)",nativeQuery = true)
	@Modifying
	void deletePieDataByReportId(Long reportId);
}
